/**
 * <p>键盘控制,示例：<body id='docbody' onKeyDown='cceaf.KeyControl.jumpNextControl(event);'></p>
 * xtype = cceafkeycontrol

 * @module Ext-ux-cceaf
 * @class cceaf.KeyControl
 * @extends Object
 * @static
 * @since 1.0
 */
Ext.ns('cceaf');

Ext.define('cceaf.KeyControl',{
	//extjs cmd只能通过类查找，需定义空类
});

cceaf.KeyControl = (function() {
	
	return {
		
		/**
		 * 跳到下一个控件
		 */
		jumpNextControl: function(event){
			var foucobj = null;
			var srcobj = event.srcElement || event.target;
		
			//alert(event.keyCode);
			
			if(event.keyCode == 13){ //回车
				if(srcobj.jump != "false" && srcobj.type != "button" && srcobj.type != "textarea"){
					foucobj = this.getNextControl();
				}
			}
			else if(event.keyCode == 39){  //右移
				var selectionLen;
				if(document.selection){
					selectionLen = srcobj.createTextRange().text.length;
				}
				else{
					if(!srcobj.value) return;
					selectionLen = srcobj.value.length;
				}
			  
				if((srcobj.type=="text" && this.getCursorPos(srcobj) == selectionLen)
						|| (srcobj.type=="textarea" && this.getCursorPos(srcobj) == selectionLen)
						||(srcobj.type != "text" && srcobj.type != "textarea"))
				{
					foucobj = this.getNextControl();
				}
			}
			else if(event.keyCode == 37){//左移键
				if((srcobj.type=="text" && this.getCursorPos(srcobj) == 0) 
						|| (srcobj.type=="textarea" && this.getCursorPos(srcobj) == 0) 
						||(srcobj.type != "text" && srcobj.type != "textarea"))
				{
					foucobj = this.getPriorControl();
				}
			}
		  	else if(event.ctrlKey && event.keyCode == 107){ //CTRL + '+'//增 加
		    	if(Ext.getCmp('btnAdd')){
		    		Ext.getCmp('btnAdd').fireEvent('click');
		    	}
			    event.keyCode = 0;
			    event.returnValue = false;
		  	}
			/*
			  else if(event.ctrlKey && event.keyCode == 83){//ctrl + S //保存数据
			     if(document.all("保 存")){
			       document.all("保 存").fireEvent("onClick");
			     }
			     event.keyCode = 0;
			     event.returnValue = false;
			  }
			  else if(window.event.ctrlKey && event.keyCode == 71){//ctrl + G //继续
			     if(document.all("继 续")){
			     	document.all("继 续").fireEvent("onClick");
			     }
			     event.keyCode = 0;
			     event.returnValue = false;
			  }
			  else if(window.event.ctrlKey && event.keyCode == 27){//ESC //返回
			     if(document.all("return")){
			     	document.all("return").fireEvent("onClick");
			     }
			     event.keyCode = 0;
			     event.returnValue = false;
			  }
			  else if(window.event.ctrlKey && event.keyCode == 88){//ctrl + Q //关闭
			     if(document.all("关 闭")){
			     	document.all("关 闭").fireEvent("onClick");
			     }
			     event.keyCode = 0;
			     event.returnValue = false;
			  }
			  else if(window.event.ctrlKey && event.keyCode == 68){//ctrl + D //删除一条
			     if(document.all("删 除")){
			     	document.all("删 除").fireEvent("onClick");
			     }
			     event.keyCode = 0;
			     event.returnValue = false;
			  }
			  else if(window.event.ctrlKey && event.keyCode == 88){//ctrl + X //全部删除
			     if(document.all("全部删除")){
			     	document.all("全部删除").fireEvent("onClick");
			     }
			     event.keyCode = 0;
			     event.returnValue = false;
			  }
			  else if(window.event.ctrlKey && event.keyCode == 80){//ctrl + P //打印
			     if(document.all("print")){
			     	document.all("print").fireEvent("onClick");
			     }
			     event.keyCode = 0;
			     event.returnValue = false;
			  }
			  else if(window.event.ctrlKey && event.keyCode == 70){//ctrl + F //查询
			     if(document.all("query")){
			     	document.all("query").fireEvent("onClick");
			     }
			     event.keyCode = 0;
			     event.returnValue = false;
			  }
			  else if(window.event.ctrlKey && event.keyCode == 82){//ctrl + R //重置
			     if(document.all("reset")){
			     	document.all("reset").fireEvent("onClick");
			     }
			     event.keyCode = 0;
			     event.returnValue = false;
			  }
			  else if(window.event.ctrlKey && event.keyCode == 69){//ctrl + E //导 出
			     if(document.all("导 出")){
			     	document.all("导 出").fireEvent("onClick");
			     }
			     event.keyCode = 0;
			     event.returnValue = false;
			  }
			  else if(window.event.ctrlKey && event.keyCode == 84){//ctrl + T //统计
			     if(document.all("统计")){
			     	document.all("统计").fireEvent("onClick");
			     }
			     event.keyCode = 0;
			     event.returnValue = false;
			  }
			  else if(event.keyCode == 33){ //pageUp
			     if(document.all("pageUp"))
			       document.all("pageUp").click();
			  }
			  else if(event.keyCode == 34){ //pageDown
			     if(document.all("pageDown"))
			       document.all("pageDown").click();
			  }
			  else if(event.keyCode == 36){ //第一页 home
			    if(document.all("pageFirst")){
			      document.all("pageFirst").click();
			    }
			  }
			  else if(event.keyCode == 35){ //最后一页 end
			    if(document.all("pageLast")){
			      document.all("pageLast").click();
			    }
			  }
		  	*/
			//alert(foucobj.name);alert(foucobj.hidden);
			
			this.focusObj(foucobj);
		},
		
		/**
		 * 取得下一个可以聚焦的控键
		 */
		getNextControl: function(){
			var e = document.activeElement;
			if(!document.forms[0]) return;
			var a = document.forms[0].elements;
			var count = a.length;
			var n = -1;
			for (var i=0; i<count; i++){
				if(a[i] == e){ n = i; break;}
			}
			if (n == -1) return;  //说明当前网页里得到焦点的项不在表单里
			if (n>=0){
				for(var i=n+1; i < count; i++){
					if(a[i].type!="hidden" && a[i].style.display!="none" && a[i].readOnly != true
							&& a[i].style.visibility != "hidden" && a[i].disabled != true){
						return a[i];
					}
				}
			}
		},
		
		/**
		 * 取得上一个可以聚焦的控键
		 */
		getPriorControl: function(){
			var e = document.activeElement;
			if(!document.forms[0]) return;
			var a = document.forms[0].elements;
			var count = a.length;
			var n = -1;
			for (var i=0; i<count; i++){
				if(a[i] == e){ n = i; break;}
			}
			if (n == -1) return;  //说明当前网页里得到焦点的项不在表单里
			if (n>=0){
				for(var i=n-1; i >= 0; i--){
					if(a[i].type!="hidden" && a[i].style.display!="none" && a[i].readOnly != true
							&& a[i].style.visibility != "hidden" && a[i].disabled != true)
					{
						return a[i];
					}
				}
			}
		},
		
		/**
		 * 聚焦控件
		 */
		focusObj: function(obj){
			if(obj){
				obj.focus();
				if(obj.type != "button" && obj.tagName != "SELECT"){
					obj.select();
				}
		    //event.returnValue = false;
			}
		},
		
		/**
		* 取得光标位置
		*/
		getCursorPos: function(obj){
			obj.focus();
			var l=0;
		
			if(obj.type=="textarea"){
				obj.select();
				if(document.selection){
					var s=document.selection.createRange();
					s.setEndPoint("StartToStart",obj.createTextRange());
					l =s.text.length;
					s.collapse(false);
					s.select();
				}
				else{
					l = obj.selectionStart;
				}
			}
			else{
				if(document.selection){
					var s=document.selection.createRange();
					s.setEndPoint("StartToStart",obj.createTextRange());
					l = s.length;
				}
				else{
					l = obj.selectionStart;
		   	   	}
			}
		   
			return l;
		}
		
	};
	
}());

